﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using Toolkit.Web.ExtensionMethods;
using System.Web;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public sealed class DataGridPaginationHelper<T> where T : class
    {
        public DataGridPaginationHelper(IReadOnlyDataGrid<T> dataGrid)
        {
            if (dataGrid == null)
            {
                throw new ArgumentNullException("dataGrid");
            }

            ReadOnlyDataGrid = dataGrid;
        }

        public IReadOnlyDataGrid<T> ReadOnlyDataGrid
        {
            get;
            private set;
        }

        public IHtmlString DefaultPageIndexSelector(int visiblePageOffset, object htmlAttributes = null)
        {
            if (ReadOnlyDataGrid.Settings.PagingType != DataGridPagingType.DefaultPaging && ReadOnlyDataGrid.Settings.PagingType != DataGridPagingType.BufferedPaging)
            {
                throw new Exception("Paging is not enabled.");
            }

            StringBuilder markup = new StringBuilder();

            if (ReadOnlyDataGrid.Settings.RecordStartIndex > 0 || (ReadOnlyDataGrid.DataSource != null && ReadOnlyDataGrid.DataSource.Count() > 0))
            {
                DataGridPagination pager = new DataGridPagination(ReadOnlyDataGrid.Settings.RecordStartIndex, ReadOnlyDataGrid.Settings.RecordSetSize.Value, ReadOnlyDataGrid.Settings.TotalRecordCount);
                pager.EnablePageIndexSelection = true;
                pager.EnablePageSizeSelection = false;
                pager.PageSizeOptions = ReadOnlyDataGrid.Settings.PageSizeOptions;
                pager.VisiblePageOffset = visiblePageOffset;
                pager.HtmlAttributes.Merge(htmlAttributes, true);

                markup.Append(pager.HtmlBuilder.Html().ToString());
            }

            return HtmlStringExtensions.Create(markup.ToString());
        }

        public IHtmlString DefaultPageSizeSelector(object htmlAttributes = null)
        {
            if (ReadOnlyDataGrid.Settings.PagingType != DataGridPagingType.DefaultPaging && ReadOnlyDataGrid.Settings.PagingType != DataGridPagingType.BufferedPaging)
            {
                throw new Exception("Paging is not enabled.");
            }

            StringBuilder markup = new StringBuilder();

            if (ReadOnlyDataGrid.Settings.RecordStartIndex > 0 || (ReadOnlyDataGrid.DataSource != null && ReadOnlyDataGrid.DataSource.Count() > 0))
            {
                DataGridPagination pager = new DataGridPagination(ReadOnlyDataGrid.Settings.RecordStartIndex, ReadOnlyDataGrid.Settings.RecordSetSize.Value, ReadOnlyDataGrid.Settings.TotalRecordCount);
                pager.EnablePageIndexSelection = false;
                pager.EnablePageSizeSelection = true;
                pager.PageSizeOptions = ReadOnlyDataGrid.Settings.PageSizeOptions;
                pager.HtmlAttributes.Merge(htmlAttributes, true);

                markup.Append(pager.HtmlBuilder.Html().ToString());
            }

            return HtmlStringExtensions.Create(markup.ToString());
        }
    }
}
